package com.xinghanlaser.applet.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinghanlaser.applet.base.auth.entity.UserDO;
import com.xinghanlaser.applet.base.auth.service.IUserService;
import com.xinghanlaser.applet.base.common.exception.ServiceException;
import com.xinghanlaser.applet.entity.UserDetail;
import com.xinghanlaser.applet.service.IUserDetailService;
import com.xinghanlaser.applet.mapper.UserDetailMapper;
import com.xinghanlaser.applet.utils.AESDecryptionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

import static com.xinghanlaser.applet.base.auth.constant.AdminConstant.DEFAULT_USER_PASSWORD;

/**
* @author XH-YF-TSDN-0080
* @description 针对表【admin_user_detail】的数据库操作Service实现
* @createDate 2024-05-20 15:54:51
*/
@Service
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail>
    implements IUserDetailService {

    @Resource
    private IUserService userService;


    @Override
    public void addUserDetail(String userName) {
        UserDO userDO = Optional.ofNullable(
                userService.getOne(Wrappers.lambdaQuery(new UserDO())
                        .select(UserDO::getId, UserDO::getUserName)
                        .eq(UserDO::getUserName, userName)
                        .last("LIMIT 1")
                )
        ).orElseThrow(() -> new ServiceException("查询新建用户失败"));

        UserDetail entity = new UserDetail();
        entity.setUserId(userDO.getId());
        entity.setUserName(userDO.getUserName());
        // 扩展字段1记录为加密后的密码，用于向认证服务器发起认证请求时解密后，作为密码传输
        entity.setExtra1(encryption(DEFAULT_USER_PASSWORD));

        this.save(entity);
    }

    @Override
    public String getPasswordByUserName(String userName) {
        UserDetail entity = Optional.ofNullable(
                this.getOne(Wrappers.lambdaQuery(new UserDetail())
                        .select(UserDetail::getUserName, UserDetail::getExtra1)
                        .eq(UserDetail::getUserName, userName)
                        .last("LIMIT 1")
                )).orElseThrow(() -> new ServiceException("获取用户详情失败"));

        return decryption(entity.getExtra1());

    }

    @Override
    public void updatePassword(String phone, String password) {
        UserDetail entity = Optional.ofNullable(
                this.getOne(Wrappers.lambdaQuery(new UserDetail())
                        .select(UserDetail::getId,UserDetail::getUserName, UserDetail::getExtra1)
                        .eq(UserDetail::getUserName, phone)
                        .last("LIMIT 1")
                )).orElseThrow(() -> new ServiceException("获取用户详情失败"));

        entity.setExtra1(encryption(password));

        this.updateById(entity);
    }

    private String encryption(String password){
        return AESDecryptionUtil.encrypt(password);

    }

    private String decryption(String encryptionPassword){
        return AESDecryptionUtil.decrypt(encryptionPassword);
    }
}




